package CrowdCounting.Model;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Util {
    private static final Logger logger = Logger.getLogger(Util.class.getName());
    static Properties properties = loadProperties("config.properties");
    public static boolean fileExistenceCheck(File file ){
        if (!file.exists()) {
            return false;
        }
        return true;
    }

    public static boolean fileExistenceCheck(String fileName ){
        if (!new File(fileName).exists()) {
            return false;
        }
        return true;
    }

    public static void deleteDirectory(File directory) {
        if (directory.exists()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        deleteDirectory(file);
                    } else {
                        file.delete();
                    }
                }
            }
            System.out.println("文件夹"+directory.getName()+"删除完毕");
            return;
        }
        throw new RuntimeException("文件夹不存在： " + directory.getName());
    }

    public static boolean checkImage(File imageFile){
        try {
            BufferedImage image = ImageIO.read(imageFile);
            if(image == null) throw new IOException(imageFile.getName());
            return true;
        } catch (IOException e) {
            System.out.println("The image data is not complete: "+imageFile.getName());
            return false;
        }
    }

    public static String loadProperty(String key) {
        return properties.getProperty(key);
    }


    public static Properties loadProperties(String config){
        Properties properties = new Properties();
        try (InputStream inputStream = new FileInputStream(config)) {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

    public static boolean createFile(String fileName) {

        File file = new File(fileName);
        if (file.exists()) return true;
        try {
            file.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return file.exists();

    }

    public static void sleepTime(int time){
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static boolean isNumeric(String str) {
        try {
            double num = Double.parseDouble(str);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

    public static void logger(Level level, String msg) {
        logger.log(level, msg + " from Thread:" + Thread.currentThread().getName());
    }

}
